#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/21 13:33
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED, ALL_COMPLETED
from functools import wraps


class ThreadPool:
    execute_dict = dict()

    def __init__(self, thread_name_prefix, max_workers=10, timeout=3000):
        self.name = thread_name_prefix
        self.timeout = timeout
        if not self.__class__.execute_dict.get(self.name, None):
            self.__class__.execute_dict[self.name] = ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix=self.name)

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            executor:ThreadPoolExecutor = self.__class__.execute_dict[self.name]
            future = executor.submit(func, *args, **kwargs);
            return future.result(timeout=self.timeout)
        return wrapped_function